#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Diagnostics;

namespace octalforty.Chinchillin.Actions.Sync
{
    /// <summary>
    /// Represents a single synchonization item.
    /// </summary>
    /// <typeparam name="TIdentity"></typeparam>
    /// <typeparam name="TChecksum"></typeparam>
    [DebuggerDisplay("Identity = {Identity,nq}, Checksum = {Checksum,nq}")]
    public class SyncItem<TIdentity, TChecksum> : IEquatable<SyncItem<TIdentity, TChecksum>>
        where TIdentity : IEquatable<TIdentity>
        where TChecksum : IEquatable<TChecksum>
    {
        #region Private Members
        private readonly Func<TChecksum> checksumProvider;
        private TChecksum checksum;
        #endregion

        #region Public Properties
        public TIdentity Identity
        { get; private set; }

        public TChecksum Checksum
        {
            get
            {
                if(Equals(checksum, default(TChecksum)))
                    checksum = checksumProvider();

                return checksum;
            }
            private set { checksum = value; }
        }
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="SyncItem{TIdentity,TChecksum}"/> class.
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="checksum"></param>
        public SyncItem(TIdentity identity, TChecksum checksum)
        {
            Identity = identity;
            Checksum = checksum;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SyncItem{TIdentity,TChecksum}"/> class.
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="checksumProvider"></param>
        public SyncItem(TIdentity identity, Func<TChecksum> checksumProvider)
        {
            Identity = identity;
            this.checksumProvider = checksumProvider;
        }

        #region IEquatable<SyncItem<TIdentity, TChecksum>> Members
        public bool Equals(SyncItem<TIdentity, TChecksum> other)
        {
            if(ReferenceEquals(null, other)) return false;
            if(ReferenceEquals(this, other)) return true;
            return Equals(other.Identity, Identity) && Equals(other.Checksum, Checksum);
        }
        #endregion

        #region Object Members
        public override bool Equals(object obj)
        {
            if(ReferenceEquals(null, obj)) 
                return false;

            if(ReferenceEquals(this, obj)) 
                return true;

            return obj.GetType() == typeof(SyncItem<TIdentity, TChecksum>) && 
                Equals((SyncItem<TIdentity, TChecksum>)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (Identity.GetHashCode() * 397) ^ Checksum.GetHashCode();
            }
        }

        public override string ToString()
        {
            return string.Format("Identity: {0}, Checksum: {1}", Identity, Checksum);
        }
        #endregion
    }

    /// <summary>
    /// Represents a factory class for creating instances of <see cref="SyncItem{TIdentity,TChecksum}"/>.
    /// </summary>
    public static class SyncItem
    {
        /// <summary>
        /// Creates an instance of the <see cref="SyncItem{TIdentity,TChecksum}"/> class.
        /// </summary>
        /// <typeparam name="TIdentity"></typeparam>
        /// <typeparam name="TChecksum"></typeparam>
        /// <param name="identity"></param>
        /// <param name="checksum"></param>
        /// <returns></returns>
        public static SyncItem<TIdentity, TChecksum> MakeSyncItem<TIdentity, TChecksum>(TIdentity identity, TChecksum checksum)
            where TIdentity : IEquatable<TIdentity>
            where TChecksum : IEquatable<TChecksum>
        {
            return new SyncItem<TIdentity, TChecksum>(identity, checksum);
        }

        /// <summary>
        /// Creates an instance of the <see cref="SyncItem{TIdentity,TChecksum}"/> class.
        /// </summary>
        /// <typeparam name="TIdentity"></typeparam>
        /// <typeparam name="TChecksum"></typeparam>
        /// <param name="identity"></param>
        /// <param name="checksumProvider"></param>
        /// <returns></returns>
        public static SyncItem<TIdentity, TChecksum> MakeSyncItem<TIdentity, TChecksum>(TIdentity identity, Func<TChecksum> checksumProvider)
            where TIdentity : IEquatable<TIdentity>
            where TChecksum : IEquatable<TChecksum>
        {
            return new SyncItem<TIdentity, TChecksum>(identity, checksumProvider);
        }
    }
}